import java.util.*;

/**
 * 测试多重括号匹配功能
 * 验证 selectExistingEnrollmentPlansWithBracketHandling 方法能否正确匹配多重括号的专业名称
 */
public class TestMultipleBracketsMatching {
    
    public static void main(String[] args) {
        // 测试用例：模拟你的具体场景
        String majorName = "中医学（省属公费医学生，面向烟台市就业）";
        String collegeMajorName = "中医学（省属公费医学生，面向烟台市就业）（免学费）";
        
        System.out.println("=== 测试多重括号匹配场景 ===");
        System.out.println("输入专业名称: " + majorName);
        System.out.println("数据库专业名称: " + collegeMajorName);
        System.out.println();
        
        // 1. 测试去括号后的匹配
        String cleanMajorName = removeBracketsFromMajorName(majorName);
        String cleanCollegeMajorName = removeBracketsFromMajorName(collegeMajorName);
        
        System.out.println("1. 去括号后匹配测试:");
        System.out.println("   输入专业去括号: " + cleanMajorName);
        System.out.println("   数据库专业去括号: " + cleanCollegeMajorName);
        System.out.println("   匹配结果: " + (cleanMajorName.equals(cleanCollegeMajorName) ? "✅ 匹配" : "❌ 不匹配"));
        System.out.println();
        
        // 2. 测试 SUBSTRING_INDEX 逻辑（模拟SQL中的逻辑）
        String substringResult = substringIndex(collegeMajorName, "（", 1);
        System.out.println("2. SUBSTRING_INDEX 匹配测试:");
        System.out.println("   SUBSTRING_INDEX('" + collegeMajorName + "', '（', 1) = '" + substringResult + "'");
        System.out.println("   与输入专业去括号比较: " + (substringResult.equals(cleanMajorName) ? "✅ 匹配" : "❌ 不匹配"));
        System.out.println();
        
        // 3. 测试 INSTR 逻辑（模拟SQL中的逻辑）
        String searchPattern = "（" + cleanMajorName;
        boolean instrResult = collegeMajorName.contains(searchPattern);
        System.out.println("3. INSTR 匹配测试:");
        System.out.println("   查找模式: '" + searchPattern + "'");
        System.out.println("   在 '" + collegeMajorName + "' 中查找");
        System.out.println("   匹配结果: " + (instrResult ? "✅ 匹配" : "❌ 不匹配"));
        System.out.println();
        
        // 4. 综合匹配结果
        boolean finalMatch = cleanMajorName.equals(substringResult) || instrResult;
        System.out.println("4. 综合匹配结果:");
        System.out.println("   最终能否匹配: " + (finalMatch ? "✅ 能匹配" : "❌ 不能匹配"));
        System.out.println();
        
        // 5. 测试更多场景
        System.out.println("=== 测试更多多重括号场景 ===");
        String[][] testCases = {
            {"日语", "日语（师范类，地方专项计划）（地方专项计划）"},
            {"药物制剂", "药物制剂(不招色盲色弱)"},
            {"英语", "英语（师范类）（免费师范生）（定向）"},
            {"数学与应用数学", "数学与应用数学（师范类）（公费师范生）（国家专项计划）"},
            {"中医学（省属公费医学生，面向烟台市就业）", "中医学（省属公费医学生，面向烟台市就业）（免学费）"}
        };
        
        for (String[] testCase : testCases) {
            String input = testCase[0];
            String database = testCase[1];
            boolean match = testMatching(input, database);
            
            System.out.println("输入: " + input);
            System.out.println("数据库: " + database);
            System.out.println("匹配: " + (match ? "✅" : "❌"));
            System.out.println("---");
        }
    }
    
    /**
     * 测试匹配逻辑（模拟SQL查询逻辑）
     */
    private static boolean testMatching(String inputMajor, String databaseMajor) {
        String cleanInput = removeBracketsFromMajorName(inputMajor);
        
        // 模拟 SUBSTRING_INDEX 逻辑
        String substringResult1 = substringIndex(databaseMajor, "(", 1);
        String substringResult2 = substringIndex(databaseMajor, "（", 1);
        
        // 模拟 INSTR 逻辑
        boolean instrMatch1 = databaseMajor.contains("(" + cleanInput);
        boolean instrMatch2 = databaseMajor.contains("（" + cleanInput);
        boolean instrMatch3 = databaseMajor.contains("、" + cleanInput);
        
        return cleanInput.equals(substringResult1) || 
               cleanInput.equals(substringResult2) || 
               instrMatch1 || instrMatch2 || instrMatch3;
    }
    
    /**
     * 模拟 MySQL 的 SUBSTRING_INDEX 函数
     */
    private static String substringIndex(String str, String delimiter, int count) {
        if (str == null || delimiter == null || count == 0) {
            return str;
        }
        
        int index = str.indexOf(delimiter);
        if (index == -1) {
            return str;
        }
        
        if (count > 0) {
            return str.substring(0, index);
        } else {
            return str.substring(index + delimiter.length());
        }
    }
    
    /**
     * 去除专业名称中的括号内容（只处理第一个括号）
     * 模拟当前系统的逻辑
     */
    private static String removeBracketsFromMajorName(String majorName) {
        if (majorName == null || majorName.trim().isEmpty()) {
            return majorName;
        }

        // 同时检查英文括号和中文括号
        int englishBracketIndex = majorName.indexOf('(');
        int chineseBracketIndex = majorName.indexOf('（');

        int bracketIndex = -1;
        if (englishBracketIndex > 0 && chineseBracketIndex > 0) {
            // 如果两种括号都存在，选择最早出现的
            bracketIndex = Math.min(englishBracketIndex, chineseBracketIndex);
        } else if (englishBracketIndex > 0) {
            bracketIndex = englishBracketIndex;
        } else if (chineseBracketIndex > 0) {
            bracketIndex = chineseBracketIndex;
        }

        if (bracketIndex > 0) {
            return majorName.substring(0, bracketIndex).trim();
        }

        return majorName;
    }
}
